﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using JDEduTrain.Framework.Entity;
using JDEduTrain.Framework.Business;

public partial class Page_SelectClassDetail : BaseAuthenticationPage
{
    public Guid ClassID
    {
        get
        {
            if (Request["ClassID"] != null)
                return new Guid(Request["ClassID"]);
            return Guid.Empty;
        }
    }

    public bool IsAudit
    {
        get
        {
            if (Request["IsAudit"] != null)
                return bool.Parse(Request["IsAudit"]);
            return false;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.UseTabber = true;
        if (!IsPostBack)
        {
            BindClass();
            if (IsAudit)
            {
                tblOperator.Visible = false;
            }
            DataTable dtOrder = BLTrain.GetOrderByClassID(ClassID);
            if (dtOrder.Rows.Count == 1)
            {
                ClassOrder order = new ClassOrder();
                order.Init(dtOrder.Rows[0]);
                lOrderInfo.Text = order.BeginTime.ToShortDateString() + "至" + order.EndTime.ToShortDateString() + ";" + order.BeginHour + "至" + order.EndHour + ";";
                tbOrderID.Text = order.ID.ToString();
                ddlOrderInfo.Visible = false;
            }
            else
            {
                lOrderInfo.Visible = false;
                foreach (DataRow dr in dtOrder.Rows)
                {
                    ClassOrder order = new ClassOrder();
                    order.Init(dr);
                    string orderStr = order.BeginTime.ToShortDateString() + "至" + order.EndTime.ToShortDateString() + ";" + order.BeginHour + "至" + order.EndHour + ";";
                    ddlOrderInfo.Items.Add(new ListItem(orderStr, order.ID.ToString()));
                }
            }
            ClassMember member = new ClassMember(CurrentUser.ID, ClassID);
            if (member.Load())
            {
                btnAttendClass.Visible = false;
                tblOperator.Visible = false;
            }
        }
    }

    protected void BindClass()
    {
        TrainClass trainClass = new TrainClass(ClassID);
        if (trainClass.Load())
        {
            tbBeginTime.Text = trainClass.BeginTime.ToShortDateString();
            tbClassName.Text = trainClass.ClassName;
            tbComment.Text = HttpUtility.HtmlDecode(trainClass.Comment);
            tbEndTime.Text = trainClass.EndTime.ToShortDateString();
            tbScore.Text = trainClass.Score.ToString();
            tbPlace.Text = trainClass.Place;
            tbLectorInfo.Text = trainClass.LectorInfo;
            tbCourseInfo.Text = trainClass.CourseInfo;
            tbTrainLevel.Text = trainClass.TrainLevel;
            tbCurriculum.Text = HttpUtility.HtmlDecode(trainClass.Curriculum);
            tbDeptDisplayName.Text = trainClass.ApplyOrgan;
            tbHostUnit.Text = trainClass.HostUnit;
            tbTrainDay.Text = trainClass.TrainDayCount.ToString();
            tbApplyEndTime.Text = trainClass.ApplyEndTime == DateTime.Now ? string.Empty : trainClass.ApplyEndTime.ToString("MM-dd");

        }



    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindClass();
    }
    protected void btnAttendClass_Click(object sender, EventArgs e)
    {
        string script;
        //if (ddlOrderInfo.Visible == true && ddlOrderInfo.SelectedValue == "请选择")
        //{
        //    script = "alert('请先选择培训时间！');";
        //}
        //else
        //{
        ClassMember member = new ClassMember(CurrentUser.ID, ClassID);
        TrainClass trainClass = new TrainClass(ClassID);

        if (member.Load())
        {
            script = "alert('您已报名！');";
        }
        else
        {
            if (ddlOrderInfo.SelectedValue != "请选择")
            {
                member.OrderID = new Guid(ddlOrderInfo.SelectedValue);
            }
            else if (!string.IsNullOrEmpty(tbOrderID.Text))
            {
                member.OrderID = new Guid(tbOrderID.Text);
            }
            member.MemberName = CurrentUser.DisplayName;
            member.MemberAccount = CurrentUser.Name;
            member.TotalScore = 0;
            member.StudyTime = 0;
            if (trainClass.Load() && trainClass.ClassName.Contains("中心组学习"))
            {
                member.Status = "终审通过";
            }
            else
            {
                member.Status = "待审核";
            }
            member.Save();
            lAttendClassName.Text = trainClass.ClassName;
            lAttendClassPlace.Text = trainClass.Place;
            lAttendScore.Text = trainClass.Score.ToString();
            script = "attendClass();";
        }
        // }
        Page.ClientScript.RegisterStartupScript(GetType(), "alert", script, true);
    }

}
